#include <allegro.h>
#include <stdlib.h>
#include <stdio.h>
#include "fonction.h"

void ini_main(personage *m, int h, int w){
	m->W=w;
	m->H=h;
	m->X=0;
	m->Y=ECRAN_Y-h;
	m->coll=0;
	m->agg=0;// 9orb entre lui et le pirate
	m->px=0; // pas d'avancement ( en fct de x)
	m->direction='d';
	m->type=0; // les types de pirates (il y en a 4 avec les boss et les mb)
	m->is_fighting=0;
	m->is_alive=1;
	m->stat.HP_MAX=10000;
	m->stat.HP=10000;
	m->stat.DPS_MIN=20;
	m->stat.DPS_MAX=100;
	m->stat.HC=70;
	m->stat.SCORE=0;
}

void ini_pirate(personage *p, int str, int typ){
	p->W=WIDTH;
	p->H=HIGHT;
	p->X=(rand() % 350)+250;
	p->Y=ECRAN_Y-HIGHT;
	p->coll=0;
	p->agg=0;
	p->px=1;
	p->direction='d';
	if(typ==-1)
	{
	p->type=(rand() % 2)+1;
	}
	else{
	p->type=typ;
	}
	p->is_fighting=0;
	p->is_alive=1;
	p->stat.HP_MAX=1000+200*str;
	p->stat.HP=1000+200*str;
	p->stat.DPS_MIN=5+2*str;
	p->stat.DPS_MAX=25+4*str;
	p->stat.HC=50+5*str;
	p->stat.SCORE=1000;
}

void fight(personage *agg, personage *vic)
{
	int r = rand() % 100;

	if(agg->is_fighting==0)
	{
		if(r<=agg->stat.HC)
		{
			r = (rand() % (agg->stat.DPS_MAX - agg->stat.DPS_MIN))+agg->stat.DPS_MIN;
			vic->stat.HP=vic->stat.HP-r;
		}

		if(vic->stat.HP<=0)
		{
			vic->is_alive=0;
		}
		agg->is_fighting=1;
	}
}

void main_fight(personage *p,personage pirate[],int nbre)
{
int i;
	for(i=0;i<nbre;i++)
	{
		if(pirate[i].is_alive==1)
		{
			if (key[KEY_ENTER]&& Collision((*p),pirate[i])!=0)
			{
				fight(p,&pirate[i]);
				if(!(pirate[i].is_alive))
				{
					set_score(p);
				}
			}
		}
	}
}

int main_move(int *x , char *d , int w)
{
	int test=0;
	if (key[KEY_A]&& (*x)!=0)
	{
		(*x)--;
		(*d)='g';
	}

	if (key [KEY_D] && (*x)!=ECRAN_X-w)
	{
		(*x)++;
		(*d)='d';
	}

	if (key [KEY_D] && (*x)==ECRAN_X-w)
	{
		test=1;
	}

	return test;
}

int Collision(personage p1, personage p2)
{
	if((p2.X > p1.X + p1.W) || (p2.X + p2.W < p1.X)) //trop à droite ou trop à gauche
		return 0;
	else
		return 1;
}

int aggro(personage p1, personage p2)
{
	if((p2.X > p1.X + p1.W+AGG_ZONE) || (p2.X + p2.W +AGG_ZONE< p1.X)) 
		return 0;
	else
		return 1;
}

int ini_alleg(){

	// function d'initialisation d'allegro
	allegro_init();

	//chargement du clavier
	if (install_keyboard() == -1)
	{
		allegro_message("Error! %s", allegro_error);
		return -1;
	}

	srand(time(NULL));

	//mattre les color en mode 16bits
	set_color_depth(32);
	
	//chargement du mode graphique
	if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, ECRAN_X, ECRAN_Y, 0, 0) != 0)
	{
		set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); 	//chargement for text mode 
		allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
		return -1;
	}

	return 0;
}

void reset_main(personage *p,int test)
{	
	if(test==1)
	p->stat.HP=p->stat.HP_MAX;
	p->X=0;
	p->is_fighting=0;
	p->direction='d';
}

void get_path(personage p,char str[])
{
	sprintf(str,"../graphs/pirate%df%d%c.bmp",p.type,p.is_fighting,p.direction);
}

void get_main_path(personage p,char str[])
{
	sprintf(str,"../graphs/%cf%d.bmp",p.direction,p.is_fighting);
}

void increment(int *lvl,int *seq)
{
	if((*seq)<4 && (*lvl)!=4)
	{
		(*seq)++;
	}
	else
	{
		(*lvl)++;
		(*seq)=0;
	}
}

void random_move(personage *p, int xmin, int xmax)
{
	// add the step to the AI position
	p->X += p->px;  

	// if the pirate reached the limit of movement zone inverse the step
	if ( p->X < xmin) 
	{
		p->px=ABS(p->px);
		p->direction='d';
	}
	if (p->X + p->W > xmax)
	{
		p->px=-ABS(p->px);
		p->direction='g'; 
	}
}

void move_to_main(personage *p,personage m)
{
	if(p->X > m.X + m.W)
	{
		(p->X)--;
		p->direction='g';
	}
	if(p->X + p->W < m.X)
	{
		(p->X)++;
		p->direction='d';
	}
}

int get_colorHP(int perc)
{
	if(perc>66)
	{
		return(makecol(0,255,0));
	}
	else if(perc>33)
	{
		return(makecol(255,128,0));
	}
	else
	{
		return(makecol(255,0,0));				
	}
}

void set_score(personage *p)
{
	p->stat.SCORE+=100;
}

int menu (BITMAP *Buffer,BITMAP *image)
{
int i=-1;
int done=0;
char path[10];

image=load_bitmap("../graphs/bleu.bmp",NULL);
blit(image, Buffer, 0, 0, 0, 0, image->w, image->h);

	while(!done)
	{
		blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
		
		if (key[KEY_S])
		{
			i++;
			if (i>3) 
				i=0;
			sprintf(path,"../graphs/m%d.bmp",i);
			image=load_bitmap(path,NULL);
			blit(image, Buffer, 0, 0, 0, 0, image->w, image->h);
		}

		if (key[KEY_W])
		{
			i--;
			if (i<0)
				i=3;
			sprintf(path,"../graphs/m%d.bmp",i);
			image=load_bitmap(path,NULL);
			blit(image, Buffer, 0, 0, 0, 0, image->w, image->h);
		}

		//exit condition
		if(key[KEY_ESC])
		{
			done=1;
			i=4;
		}

		if(key[KEY_ENTER])
		{
			done=1;
		}
		usleep(110000);
	}
	return i;
}
